// pages/index/index.js
Page({

  data: {
    loading: true,
    staff: false,
  },

  onLoad() {
    wx.login({
      success: code => {
        wx.post("wxStaffLogin", {
          code: code.code
        }, res => {
          this.setData({ loading: false })
          if (res.ok) {
            this.setData({ staff: true })
            wx.appData.staff = res.data
          }
        })
        
        // wx.post("wxCreateOrder", {
        //   code: code.code,
        //   price: 1,
        //   description: '123'
        // }, res => {
        //   wx.requestPayment({
        //     signType: 'RSA',
        //     nonceStr: 'chjl1234chjl1234chjl1234chjl1234',
        //     package: `prepay_id=`+res.data.data.prepay_id,
        //     paySign: res.data.data.sign,
        //     timeStamp: new Date().getTime().toString(),
        //   })
        // });
      }
    })
    
  },
  
  toList(e) {
    this.login(res => {
      wx.nav('../staff/list/list');
    }, '正在获取列表')
  },
  toTask(e) {
    this.login(res => {
      wx.nav('../staff/task/task');
    }, '正在查询订单')
  },
  toBuy(e) {
    this.login(res => {
      wx.nav('../user/buy/buy');
    }, '正在创建订单')
  },
  toInfo() {
    this.login(res => {
      wx.nav('../user/myinfo/myinfo');
    }, '正在读取信息')
  },
  toOrders() {
    this.login(res => {
      wx.nav('../user/orders/orders');
    }, '正在查询订单')
  },
  toText(e) {
    wx.nav('../user/text/text');
  },
  login (callback, loadingText) {
    wx.login({
      success: code => {
        wx.getUserInfo({
          lang: 'zh_CN',
          withCredentials: true,
          success: info => {
            wx.post('wxUserLogin', {
              encryptedData: info.encryptedData,
              iv: info.iv,
              code: code.code,
              tag: 0,
            }, res => { 
              wx.appData.user = res.data;
              callback(res); 
            }, loadingText)
          },
          fail: (res) => {
            wx.toast('用户没有授权');
          },
        })
      }
    })
  },


  onShareAppMessage: function () {

  },

  // of no use:

  denglu(url, tag, cb) {
    wx.login({
      success: code => {
        wx.getUserInfo({
          lang: 'zh_CN',
          withCredentials: true,
          success: info => {
            wx.request({
              url: url,
              data: {
                encryptedData: info.encryptedData,
                iv: info.iv,
                code: code.code,
                tag: tag,
              },
              method: 'POST',
              header: { 'content-type': 'application/json' },
              success: res => {
                cb(res);
              },
            })
          },
          fail: (res) => {
            wx.toast('用户没有授权');
          },
        })
      }
    })
  },

})